#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include <linux/rtc.h>

typedef struct {
  unsigned int  year;
  unsigned int  month;
  unsigned int  date;
  unsigned int  hour;
  unsigned int  minute;
  unsigned int  second;
  unsigned int  weekday;
} rtc_time_t;

typedef	struct {
	unsigned char reg;
	unsigned char val;
} reg_data_t;

#define HI_RTC_RD_TIME		_IOR('p', 0x09,  rtc_time_t)
#define HI_RTC_SET_TIME		_IOW('p', 0x0a,  rtc_time_t)
#define HI_RTC_REG_READ		_IOR('p', 0x0d,  reg_data_t)

int rtc_main(void)
{
  rtc_time_t tm;
  int ret = -1;
  int fd = -1;
  const char *dev_name = "/dev/hi_rtc";

  fd = open(dev_name, O_RDWR);
  if (fd < 0) {
    printf("open %s failed\n", dev_name);
    return -1;
  }

  memset(&tm, 0, sizeof(tm));
  tm.year = 2016;
  tm.month = 9;
  tm.date = 18;
  tm.hour = 9;
  tm.minute = 0;
  tm.second = 0;

  ret = ioctl(fd, HI_RTC_SET_TIME, &tm);
  if (ret < 0) {
    printf("ioctl: HI_RTC_SET_TIME failed\n");
  }	
  close(fd);

  return 0;
}

int ds3231_main(void)
{
  struct tm tm;
  int ret = -1;
  int fd = -1;
  const char *dev_name = "/dev/ds3231_rtc";

  fd = open(dev_name, O_RDWR);
  if (fd < 0) {
    printf("open %s failed\n", dev_name);
    return -1;
  }

  memset(&tm, 0, sizeof(tm));
  tm.tm_year = 2016;
  tm.tm_mon = 9;
  tm.tm_mday = 18;
  //tm.tm_wday = 6;
  tm.tm_hour = 9;
  tm.tm_min = 0;
  tm.tm_sec = 0;

  ret = ioctl(fd, RTC_SET_TIME, &tm);
  if (ret < 0) {
    printf("ioctl: (ds3231) RTC_SET_TIME failed\n");
  }	
  close(fd);

  return 0;
}

int main(int argc, char *argv[])
{
  int r_fd;
  int d_fd;
  rtc_time_t tm;
  reg_data_t regv;
  struct tm time;
  int ret = -1;

  if (argc < 2) {
    printf("usage %s [-s (set time) | -g (get time)]\n", argv[0]);
    exit(0);
  }

  memset(&tm, 0, sizeof(tm));
	
  if (!strcmp(argv[1], "-s")) {
    rtc_main();
    ds3231_main();
  } else if (!strcmp(argv[1], "-g")) {
	
    r_fd = open("/dev/hi_rtc", O_RDWR);
    if (r_fd < 0) {
      printf("open hi rtc failed\n");
      return -1;
    }

    d_fd = open("/dev/ds3231_rtc", O_RDWR);
    if (d_fd < 0) {
      printf("open ds3231 failed\n");
      return -1;
    }

    while (1) {
      ret = ioctl(r_fd, HI_RTC_RD_TIME, &tm);
      if (ret < 0) {
		printf("ioctl: HI_RTC_RD_TIME failed\n");
      }
	  
	  regv.reg = 0x00;
	  regv.val = 0;
	   ret = ioctl(r_fd, HI_RTC_REG_READ, &regv);
      if (ret < 0) {
		printf("ioctl: HI_RTC_RD_TIME failed\n");
      }	
	  
      ret = ioctl(d_fd, RTC_RD_TIME, &time);
      if (ret < 0) {
		printf("ioctl: (ds3231)RTC_RD_TIME failed\n");
      }

      printf("RTC:%02d:%02d:%02d:%02d\n", tm.hour, tm.minute, tm.second, regv.val);
      printf("DST:%02d:%02d:%02d\n", time.tm_hour, time.tm_min, time.tm_sec);

      //sleep(1);
    }
  } else
		printf("usage %s [-s (set time) | -g (get time)]\n", argv[0]);
}
